﻿using System;
using UnityEngine;

// Token: 0x0200035F RID: 863
public static class ShaderLoader
{
	// Token: 0x0600164A RID: 5706 RVA: 0x00090F6C File Offset: 0x0008F16C
	public static Shader GetShader(string shaderName)
	{
		if (shaderName.Contains("Standard"))
		{
			if (shaderName == "Standard")
			{
				shaderName = "SB_Standard";
			}
			else if (shaderName == "StandardCullOff" || shaderName == "Standard2Sided")
			{
				shaderName = "SB_Standard2Sided";
			}
			else if (shaderName == "Standard (Specular setup)")
			{
				shaderName = "SB_StandardSpecular";
			}
		}
		Shader shader = Shader.Find(shaderName);
		if (shader != null && shader.isSupported)
		{
			return shader;
		}
		return Shader.Find("SB_Standard2Sided");
	}

	// Token: 0x0600164B RID: 5707 RVA: 0x00091014 File Offset: 0x0008F214
	public static void SetShaderOnMaterial(Material material, string shaderName)
	{
		if (material == null)
		{
			Debug.Log("No material to set");
		}
		Shader shader = ShaderLoader.GetShader(shaderName);
		string name = shader.name;
		material.shader = shader;
		if (name.Contains("Standard"))
		{
			ShaderLoader.BlendMode blendMode = (ShaderLoader.BlendMode)material.GetFloat("_Mode");
			ShaderLoader.SetupMaterialWithBlendMode(material, blendMode);
		}
		else if (name.Contains("MMD"))
		{
			Color color = material.GetColor("_Ambient");
			color *= 1.4f;
			material.SetColor("_Ambient", color);
		}
	}

	// Token: 0x0600164C RID: 5708 RVA: 0x000910AC File Offset: 0x0008F2AC
	public static void SetupMaterialWithBlendMode(Material material, ShaderLoader.BlendMode blendMode)
	{
		switch (blendMode)
		{
		case ShaderLoader.BlendMode.Opaque:
			material.SetFloat("_Mode", 0f);
			material.SetInt("_SrcBlend", 1);
			material.SetInt("_DstBlend", 0);
			material.DisableKeyword("_ALPHATEST_ON");
			material.DisableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.SetInt("_ZWrite", 1);
			material.renderQueue = -1;
			break;
		case ShaderLoader.BlendMode.Cutout:
			material.SetFloat("_Mode", 1f);
			material.SetInt("_SrcBlend", 1);
			material.SetInt("_DstBlend", 0);
			material.EnableKeyword("_ALPHATEST_ON");
			material.DisableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.SetInt("_ZWrite", 1);
			material.renderQueue = 2450;
			break;
		case ShaderLoader.BlendMode.Fade:
			material.SetFloat("_Mode", 2f);
			material.SetInt("_SrcBlend", 5);
			material.SetInt("_DstBlend", 10);
			material.DisableKeyword("_ALPHATEST_ON");
			material.EnableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.SetInt("_ZWrite", 0);
			material.renderQueue = 3000;
			break;
		case ShaderLoader.BlendMode.Transparent:
			material.SetFloat("_Mode", 3f);
			material.SetInt("_SrcBlend", 1);
			material.SetInt("_DstBlend", 10);
			material.DisableKeyword("_ALPHATEST_ON");
			material.DisableKeyword("_ALPHABLEND_ON");
			material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
			material.SetInt("_ZWrite", 0);
			material.renderQueue = 3000;
			break;
		}
	}

	// Token: 0x0600164D RID: 5709 RVA: 0x00091268 File Offset: 0x0008F468
	public static void ApplyShader(GameObject go, Shader shader)
	{
		Renderer[] componentsInChildren = go.GetComponentsInChildren<Renderer>();
		foreach (Renderer renderer in componentsInChildren)
		{
			Material[] sharedMaterials = renderer.sharedMaterials;
			foreach (Material material in sharedMaterials)
			{
				string name = material.name;
				string name2 = material.shader.name;
				material.shader = shader;
				int num = (int)material.GetFloat("_Mode");
				if (name2.Contains("Standard"))
				{
					ShaderLoader.BlendMode blendMode = (ShaderLoader.BlendMode)num;
					ShaderLoader.SetupMaterialWithBlendMode(material, blendMode);
				}
				else if (name.Contains("eyes") && !name.Contains("_"))
				{
					ShaderLoader.SetupMaterialWithBlendMode(material, ShaderLoader.BlendMode.Opaque);
				}
				else if (name.Contains("eye") || name.Contains("matuge") || name.Contains("hitomi") || (name.Contains("matsuge") | name.Contains("shadow")) || name.Contains("shadou") || name.Contains("scar") || name.Contains("kage") || name.Contains("hairaito") || name.Contains("kizu") || name.Contains("mune2"))
				{
					ShaderLoader.SetupMaterialWithBlendMode(material, ShaderLoader.BlendMode.Fade);
				}
				else if (name.Contains("Lens") || name.Contains("renzu"))
				{
					ShaderLoader.SetupMaterialWithBlendMode(material, ShaderLoader.BlendMode.Transparent);
				}
				else if (name.Contains("Face"))
				{
					ShaderLoader.SetupMaterialWithBlendMode(material, ShaderLoader.BlendMode.Cutout);
				}
				else
				{
					ShaderLoader.BlendMode blendMode2 = (ShaderLoader.BlendMode)num;
					bool flag = false;
					if (material.color.a == 0f)
					{
						flag = true;
					}
					else if (blendMode2 == ShaderLoader.BlendMode.Opaque && material.mainTexture != null)
					{
						flag = TextureLoader.IsTransparent(material.mainTexture);
					}
					if (flag)
					{
						ShaderLoader.SetupMaterialWithBlendMode(material, ShaderLoader.BlendMode.Cutout);
					}
					else
					{
						ShaderLoader.SetupMaterialWithBlendMode(material, blendMode2);
					}
				}
			}
		}
	}

	// Token: 0x0600164E RID: 5710 RVA: 0x000914C0 File Offset: 0x0008F6C0
	public static void EnableMaterialMorph(GameObject go)
	{
		Renderer[] componentsInChildren = go.GetComponentsInChildren<Renderer>();
		foreach (Renderer renderer in componentsInChildren)
		{
			Material[] sharedMaterials = renderer.sharedMaterials;
			foreach (Material material in sharedMaterials)
			{
				if (material.shader.name.Contains("Standard") && (int)material.GetFloat("_Mode") == 0 && (double)material.color.a > 0.5 && !TextureLoader.IsTransparent(material.mainTexture))
				{
					ShaderLoader.SetupMaterialWithBlendMode(material, ShaderLoader.BlendMode.Cutout);
				}
			}
		}
	}

	// Token: 0x04001901 RID: 6401
	private const string standard = "Standard";

	// Token: 0x04001902 RID: 6402
	private const string standardCullOff = "StandardCullOff";

	// Token: 0x04001903 RID: 6403
	private const string standard2Sided = "Standard2Sided";

	// Token: 0x04001904 RID: 6404
	private const string standardSpecular = "Standard (Specular setup)";

	// Token: 0x04001905 RID: 6405
	private const string sbStandard = "SB_Standard";

	// Token: 0x04001906 RID: 6406
	private const string sbStandard2Sided = "SB_Standard2Sided";

	// Token: 0x04001907 RID: 6407
	private const string sbStandardSpecular = "SB_StandardSpecular";

	// Token: 0x04001908 RID: 6408
	private const string sMode = "_Mode";

	// Token: 0x04001909 RID: 6409
	private const string sSrcBlend = "_SrcBlend";

	// Token: 0x0400190A RID: 6410
	private const string sDstBlend = "_DstBlend";

	// Token: 0x0400190B RID: 6411
	private const string sAlphaTestOn = "_ALPHATEST_ON";

	// Token: 0x0400190C RID: 6412
	private const string sAlphaBlendOn = "_ALPHABLEND_ON";

	// Token: 0x0400190D RID: 6413
	private const string sAlphaPremultipyOn = "_ALPHAPREMULTIPLY_ON";

	// Token: 0x0400190E RID: 6414
	private const string sZWrite = "_ZWrite";

	// Token: 0x02000360 RID: 864
	public enum BlendMode
	{
		// Token: 0x04001910 RID: 6416
		Opaque,
		// Token: 0x04001911 RID: 6417
		Cutout,
		// Token: 0x04001912 RID: 6418
		Fade,
		// Token: 0x04001913 RID: 6419
		Transparent
	}
}
